package com.ehl.capture.tool;

import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static String getNowTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    public static String getNowDateWithMinute() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        return sdf.format(new Date());
    }

    public static String getNowDateWithHour() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        return sdf.format(new Date());
    }

    public static String subTimeMinute(String time, String frmT, Object minute){
        SimpleDateFormat sdf = new SimpleDateFormat(frmT);
        java.util.Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        date.setMinutes(date.getMinutes() - Integer.parseInt(minute.toString()));
        return sdf.format(date);
    }

    public static Date StringToDate(String string, boolean withtime){
        if (StringUtils.isBlank(string)) {
            return null;
        }
        SimpleDateFormat sdf;
        if (withtime)
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        else
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        try{
            return sdf.parse(string);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static java.sql.Date convertToSqlDate(Date date) {
        try{
            if (date == null) {
                return null;
            }
            java.sql.Date sqlDate = new java.sql.Date(date.getTime());
            return sqlDate;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;
    }

    public static Timestamp convertToTimestamp(Date date){
        try{
            if (date == null) {
                return null;
            }
            return new Timestamp(date.getTime());
        }catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static String getDateBeforeNow(int modifyHours) {
        Calendar cal = Calendar.getInstance();
        cal.add(11, -1 * modifyHours);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        return sdf.format(cal.getTime());
    }

    public static Date addMinute(Date now, int minutes) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(12, minutes);
        return cal.getTime();
    }

    public static String DateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static Date StringToDate(String str, String format) {
        Date date = null;
        if ((str == null) || (str.equals(""))) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            date = sdf.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String DateToString(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static boolean isBefore(String dateStr, int days) {
        dateStr = dateStr.substring(0, 19);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(5, -1 * days);
        return cal.getTimeInMillis() > date.getTime();
    }

    public static void main(String[] args)
    {
        System.out.println(isBefore("2015-01-18 00:00:00", 3));
    }
}